package com.ruoyi.system.service.impl;

import java.util.List;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import com.ruoyi.system.domain.Product;
import com.ruoyi.system.domain.Repertory;
import com.ruoyi.system.mapper.ProductMapper;
import com.ruoyi.system.mapper.RepertoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SellMapper;
import com.ruoyi.system.domain.Sell;
import com.ruoyi.system.service.ISellService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 销售信息Service业务层处理
 * 
 * @author 徐国洋
 * @date 2025-02-24
 */
@Service
public class SellServiceImpl implements ISellService 
{
//    private static final Logger logger = LoggerFactory.getLogger(SellServiceImpl.class);
    @Autowired
    private SellMapper sellMapper;

    @Autowired
    private RepertoryMapper repertoryMapper;

    @Autowired
    private ProductMapper productMapper;

    /**
     * 查询销售信息
     * 
     * @param id 销售信息主键
     * @return 销售信息
     */
    @Override
    public Sell selectSellById(Long id)
    {
        return sellMapper.selectSellById(id);
    }

    /**
     * 查询销售信息列表
     * 
     * @param sell 销售信息
     * @return 销售信息
     */
    @Override
    public List<Sell> selectSellList(Sell sell)
    {
        return sellMapper.selectSellList(sell);
    }

    /**
     * 新增销售信息
     * 
     * @param sell 销售信息
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertSell(Sell sell)
    {
        int result = sellMapper.insertSell(sell);
        Product product = productMapper.selectProductById(sell.getPid());
        //查询添加在销售表里物品的库存
        Repertory repertory = repertoryMapper.selectRepertoryById(sell.getPid());
        if(product.getRate()==null){
            throw new RuntimeException("请先设置商品价格");
        }
        sell.setMoneysum(product.getRate().multiply(new java.math.BigDecimal(sell.getSellmount())));
        sellMapper.updateSell(sell);
        //卖出的数量
        Long Smount =sell.getSellmount();
        //库存数量
        Long Rmount = repertory.getStorage();

        if(Rmount<Smount){
            throw new RuntimeException("库存不足");
        }
        repertory.setStorage(Rmount-Smount);
        //更新库存
        repertoryMapper.updateRepertory(repertory);

        return result;
    }

    /**
     * 修改销售信息
     * 
     * @param sell 销售信息
     * @return 结果
     */
    @Override
    public int updateSell(Sell sell)
    {
//        int result = sellMapper.updateSell(sell);
//        //修改的数量
//        Long Smount =sell.getSellmount();
//        //获取最开始销售表里修修改前原本的数量
//        Sell oldSell =sellMapper.selectSellById(sell.getId());
//        Long SellMount = oldSell.getSellmount();
//        //修改的数量差值
//        Long diff;
//        //查询修改在销售表里物品的库存
//        Repertory repertory = repertoryMapper.selectRepertoryById(sell.getPid());
//        if(Smount == SellMount){
//             return 0;
//        } else if (SellMount>Smount) {
//            diff = SellMount - Smount;
//            repertory.setStorage(repertory.getStorage()+diff);
//            repertoryMapper.updateRepertory(repertory);
//        }else {
//            diff = Smount - SellMount;
//            // 检查库存是否足够
//            if (repertory.getStorage() < diff) {
//                throw new RuntimeException("库存不足");
//        }
//            repertory.setStorage(repertory.getStorage() - diff);
//            repertoryMapper.updateRepertory(repertory);
//        }
        return sellMapper.updateSell(sell);
    }

    /**
     * 批量删除销售信息
     * 
     * @param ids 需要删除的销售信息主键
     * @return 结果
     */
    @Override
    public int deleteSellByIds(Long[] ids)
    {
        return sellMapper.deleteSellByIds(ids);
    }

    /**
     * 删除销售信息信息
     * 
     * @param id 销售信息主键
     * @return 结果
     */
    @Override
    public int deleteSellById(Long id)
    {
        return sellMapper.deleteSellById(id);
    }
}
